var app = getApp();
Page({
  data: {
    questionList:[],
    index: 0,  // 题目序列
    chooseValue: [], // 选择的答案序列
    score: 10, // 错误的题目数量
    wrongList: [], // 错误的题目集合-乱序
    trueValue:[],
    isChecked: false
  },
  onLoad: function (options) {

    wx.request({
      url: 'http://101.35.134.51:8080/base-in-back-end/app/wechatQuestion/findOne',
      data:{},
      success:res=>{
        console.log(res.data)
          this.setData(
              {
                  'questionList[0]':res.data
              }
        )

        
      }
    })


    wx.request({
      url: 'http://101.35.134.51:8080/base-in-back-end/app/wechatQuestion/findOne',
      data:{},
      success:res=>{
        console.log(res.data)
          this.setData(
              {
                  'questionList[1]':res.data
              }
        )

        
      }
    })

    wx.request({
      url: 'http://101.35.134.51:8080/base-in-back-end/app/wechatQuestion/findOne',
      data:{},
      success:res=>{
        console.log(res.data)
          this.setData(
              {
                  'questionList[2]':res.data
              }
        )

        
      }
    })

    wx.request({
      url: 'http://101.35.134.51:8080/base-in-back-end/app/wechatQuestion/findOne',
      data:{},
      success:res=>{
        console.log(res.data)
          this.setData(
              {
                  'questionList[3]':res.data
              }
        )

        
      }
    })

    wx.request({
      url: 'http://101.35.134.51:8080/base-in-back-end/app/wechatQuestion/findOne',
      data:{},
      success:res=>{
        console.log(res.data)
          this.setData(
              {
                  'questionList[4]':res.data
              }
        )

        
      }
    })

    wx.request({
      url: 'http://101.35.134.51:8080/base-in-back-end/app/wechatQuestion/findOne',
      data:{},
      success:res=>{
        console.log(res.data)
          this.setData(
              {
                  'questionList[5]':res.data
              }
        )

        
      }
    })

    wx.request({
      url: 'http://101.35.134.51:8080/base-in-back-end/app/wechatQuestion/findOne',
      data:{},
      success:res=>{
        console.log(res.data)
          this.setData(
              {
                  'questionList[6]':res.data
              }
        )

        
      }
    })

    wx.request({
      url: 'http://101.35.134.51:8080/base-in-back-end/app/wechatQuestion/findOne',
      data:{},
      success:res=>{
        console.log(res.data)
          this.setData(
              {
                  'questionList[7]':res.data
              }
        )

        
      }
    })

    wx.request({
      url: 'http://101.35.134.51:8080/base-in-back-end/app/wechatQuestion/findOne',
      data:{},
      success:res=>{
        console.log(res.data)
          this.setData(
              {
                  'questionList[8]':res.data
              }
        )

        
      }
    })

    wx.request({
      url: 'http://101.35.134.51:8080/base-in-back-end/app/wechatQuestion/findOne',
      data:{},
      success:res=>{
        console.log(res.data)
          this.setData(
              {
                  'questionList[9]':res.data
              }
        )

        
      }
    })

  },
 
  radioChange: function(e){
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    this.data.chooseValue[this.data.index] = e.detail.value;
    this.data.trueValue[this.data.index] = this.data.questionList[this.data.index].data.rightOption;
    console.log(this.data.chooseValue);
    console.log(this.data.trueValue);
  },
  /*
  * 退出答题 按钮
  */
  outTest: function(){
    wx.showModal({
      title: '提示',
      content: '你真的要退出答题吗？',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.switchTab({
            url: '../home/home'
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /*
  * 下一题/提交 按钮
  */
  nextSubmit: function(){
    // 如果没有选择
    if (this.data.chooseValue[this.data.index] == undefined || this.data.chooseValue[this.data.index].length == 0) {
      wx.showToast({
        title: '请选择至少一个答案!',
        icon: 'none',
        duration: 2000,
        success: function () {
          return;
        }
      })
      return;
    }

    // 判断答案是否正确
    this.chooseError();

    // 判断是不是最后一题
    if (this.data.index < 9) {
      // 渲染下一题
      this.setData({
        index: this.data.index + 1,
        isChecked: false
      })
    } else {
      let questionList = JSON.stringify(this.data.questionList);
      let wrongList = JSON.stringify(this.data.wrongList);
      let chooseValue = JSON.stringify(this.data.chooseValue);
      let trueValue = JSON.stringify(this.data.trueValue);
      wx.navigateTo({
        url: '../results/results?trueValue=' + trueValue + '&wrongList=' + wrongList + '&chooseValue=' + chooseValue + '&questionList=' + questionList+'&score='+this.data.score
      })

      //缓存
       var logs = wx.getStorageSync('logs') || []
       let logsList = { "date": Date.now(), "score": this.data.score }
       logs.unshift(logsList);
       wx.setStorageSync('logs', logs);
    }
  },
  /*
  * 错题处理
  */
  chooseError: function(){
    var trueVal = this.data.trueValue[this.data.index];
    var chooseVal = this.data.chooseValue[this.data.index];
    console.log('选择了' + chooseVal + '答案是' + trueVal);
    if (chooseVal.toString() != trueVal.toString()) {
      console.log('错了');
      this.data.score--;
      this.data.wrongList.push(this.data.index);
    }
  },
  /**
     * 生成一个从 start 到 end 的连续数组
     * @param start
     * @param end
     */
  generateArray: function(start, end) {
    return Array.from(new Array(end + 1).keys()).slice(start)
  }
})